<?php

/**
 * Menu
 * 
 * @author MF
 * @version 2.0
 */

require_once 'Zend/Controller/Action.php';

class Cms_MenuController extends Zend_Controller_Action {
	
	const module_name = 'menu';

	public function indexAction() {
		throw new Zend_Controller_Action_Exception('Index not set', 404);
	}
	
	public function getAction() {
		CmsAuth::check();
		
		if (NULL === $id = $this->getRequest()->getParam('id')) {
			throw new Zend_Controller_Action_Exception('Required param "Id"', 404);
		}
		
		$this->view->MenuBranch = MF_Main::getInstance()->Menu->getPointById($id);
		$this->_helper->layout->disableLayout();
	}
	
	public function addformAction() {
		if (NULL === $root_id = $this->getRequest()->getParam('root_id')) {
			throw new Zend_Controller_Action_Exception('Required param "root_id"', 404);
		}
		
		CmsAccess::restrictMenu($root_id);
		CmsAccess::restrictModule(self::module_name);
		
		$main = MF_Main::getInstance();
		
		$this->view->title = "Добавление раздела меню";
		$this->view->menu_type_list = $main->Menu->menuTypes;
		
		$template = $main->Template;
		$template_key_list = $template->getList();
		$template_list = array();
		for ($t=0;$t<count($template_key_list);$t++) {
			$template_list[] = array('key'=>$template_key_list[$t],'value'=>$template->getAttr($template_key_list[$t],'name'));
		}
		$this->view->current_template = $template->defaul_template;
		$this->view->template_list = $template_list;
		
		
		$this->view->post_data = array('menu_type_id' => '1');
		
		if ($this->_request->isPost()) {
			$post_data = $this->_request->getPost();
			
			$form_validator = new CmsMenuForm($post_data);
			if (!$form_validator->isValid()) {
				$this->view->errors = $form_validator->getErrors();
				$this->view->post_data = $post_data;
				$this->view->current_template = $post_data['template'];
			} else {
				$new_menu = MF_Main::getInstance()->Menu
					->getPointById($root_id)->add();
				$new_menu->name = $post_data['name'];
				$new_menu->menu_type_id = $post_data['menu_type_id'];
				switch ($post_data['menu_type_id']) {
					case '1':
						$new_menu->alias = $post_data['alias'];
						$new_menu->page->template = $post_data['template'];
						break;
					
					case '3':
						$new_menu->direct_url = $post_data['direct_url'];
						break;
						
					default:
						throw new Zend_Controller_Action_Exception('Passed invalid "menu_type_id" param value while adding new menu item', 404);;
						break;
				}
				
				
				$this->renderScript('exit.phtml');
				return true;
				
			}
			
		}
		
		$this->render('form');
	}
	
	public function editformAction() {
		
		if (NULL === $menu_id = $this->getRequest()->getParam('menu_id')) {
			throw new Zend_Controller_Action_Exception('Required param "menu_id"', 404);
		}
		
		CmsAccess::restrictMenu($menu_id);
		CmsAccess::restrictModule(self::module_name);
		
		$main = MF_Main::getInstance();
		
		$this->view->title = "Изменение раздела меню";
		$this->view->menu_type_list = $main->Menu->menuTypes;
		
		$template = $main->Template;
		$template_key_list = $template->getList();
		$template_list = array();
		for ($t=0;$t<count($template_key_list);$t++) {
			$template_list[] = array('key'=>$template_key_list[$t],'value'=>$template->getAttr($template_key_list[$t],'name'));
		}
		$this->view->template_list = $template_list;
		
		$menuItem = $main->Menu->getPointById($menu_id);
		$this->view->post_data = 
			array(
				'name'  => $menuItem->name,
				'alias' => $menuItem->alias,
				'menu_type_id' => $menuItem->menu_type_id
			);
		$this->view->current_template = $menuItem->page->template_name;
		$this->view->template_alert = true;
		
		
		if ($this->_request->isPost()) {
			$post_data = $this->_request->getPost();
			$form_validator = new CmsMenuForm($post_data,$menu_id);
			if (!$form_validator->isValid()) {
				$this->view->errors = $form_validator->getErrors();
				$this->view->post_data = $post_data;
				$this->view->current_template = $post_data['template'];
			} else {
				$menuItem->name = $post_data['name'];
				$menuItem->menu_type_id = $post_data['menu_type_id'];
				switch ($post_data['menu_type_id']) {
					case '1':
						$menuItem->alias = $post_data['alias'];
						if ($menuItem->page->template_name!=$post_data['template']) {
							$menuItem->page->template = $post_data['template'];
						}
						break;
					
					case '3':
						$menuItem->direct_url = $post_data['direct_url'];
						break;
						
					default:
						throw new Zend_Controller_Action_Exception('Passed invalid "menu_type_id" param value while editing menu properties', 404);;
						break;
				}
				
				
				
				
				$this->renderScript('exit.phtml');
				return true;
				
			}
			
		}
		
		$this->render('form');
	}
	
	public function moveAction() {
		if (NULL === $id = $this->getRequest()->getParam('id')) {
			throw new Zend_Controller_Action_Exception('Required param "Id"', 404);
		}
		
		if (NULL === $dir = $this->getRequest()->getParam('direction')) {
			throw new Zend_Controller_Action_Exception('Required param "Direction"', 404);
		} 
		
		CmsAccess::restrictMenu($id);
		CmsAccess::restrictModule(self::module_name);
		
		try {
			$menuItem = MF_Main::getInstance()->Menu->getPointById($id);
			switch ($dir) {
				case 'up':
					if ($menuItem->sequence > 1) {
						$menuItem->sequence = $menuItem->sequence - 1;
					}
					break;
				
				case 'down':
					if ($menuItem->sequence < $menuItem->parent->length) {
						$menuItem->sequence = $menuItem->sequence+1;
					}
					break;
				
				default:
					throw new Zend_Controller_Action_Exception('Invalid value for param "Direction"', 404);
					break;
			}
			
			
			$this->view->response = 'y';
		} catch (PushError $e) {
			$this->view->response = $e->getUserMessage();
		}
		
		$this->_helper->layout->disableLayout();
	}
	
	public function deleteAction() {
		if (NULL === $id = $this->getRequest()->getParam('id')) {
			throw new Zend_Controller_Action_Exception('Required param "Id"', 404);
		}
		
		CmsAccess::restrictMenu($id);
		CmsAccess::restrictModule(self::module_name);
		
		try {
			MF_Main::getInstance()->Menu->getPointById($id)->delete();
			
			// TODO реализовать удаление всех прав на меню
			
			$this->view->response = 'y';
		} catch (PushError $e) {
			$this->view->response = $e->getUserMessage();
		}
		
		$this->_helper->layout->disableLayout();
	}
		
	public function pathAction() {
		CmsAuth::check();
		if (NULL === $id = $this->getRequest()->getParam('id')) {
			throw new Zend_Controller_Action_Exception('Required param "Id"', 404);
		}
		
		$menu_path = array();
		$current_item = MF_Main::getInstance()->Menu->getPointById($id);
		array_push($menu_path,$current_item);
	
		while (true) {
			try {
				$current_item = $current_item->parent;
				array_push($menu_path,$current_item);
			} catch (Exception $e) {
				break;
			}
		}
		
		$this->view->menuPath = array_reverse($menu_path);
		
		
		$this->_helper->layout->disableLayout();
	}
	
	public function ifexsistsAction() {
		CmsAuth::check();
		if (NULL === $id = $this->getRequest()->getParam('id')) {
			throw new Zend_Controller_Action_Exception('Required param "Id"', 404);
		}
		
		try {
			MF_Main::getInstance()->Menu->getPointById($id);
			$this->view->response = 'y';
		} catch (Exception $e) {
			$this->view->response = 'n';
		}
		
		$this->_helper->layout->disableLayout();
	}
	
	public function infoAction() {
		CmsAuth::check();
		if (NULL === $id = $this->getRequest()->getParam('menu_id')) {
			throw new Zend_Controller_Action_Exception('Required param "Id"', 404);
		}
		$menuInstance = MF_Main::getInstance()->Menu;
		$menuItem = $menuInstance->getPointById($id); 
		
		if (CmsAccess::checkMenu($menuItem) && CmsAccess::checkModule('menu')) {
			$avalible_actions = $menuItem->menu_id==$menuInstance->rootPoint->menu_id ? 
				array('add') : 
				array('add','edit','up','down','del','properties','link');
		} else {
			$avalible_actions = array('link');
		}
		
		if (CmsAccess::checkModule('administrators')) {
			array_push($avalible_actions,'access');
		}
		
		$test = array(
				'avlible_actions' => $avalible_actions ,
				'root_menu_id' => $menuItem->root_menu_id
			);
			
		$this->view->response = Zend_Json::encode($test);
		
		$this->_helper->layout->disableLayout();
	}
	
	public  function propertiesformAction() {
		if (NULL === $id = $this->getRequest()->getParam('menu_id')) {
			throw new Zend_Controller_Action_Exception('Required param "Id"', 404);
		}
		
		CmsAccess::restrictMenu($id);
		CmsAccess::restrictModule(self::module_name);
		
		$menuItem = MF_Main::getInstance()->Menu->getPointById($id); 
		
		$flags = $menuItem->flag;
		
		$this->view->is_hidden = $flags->is_hidden;
		
		if ($this->_request->isPost()) {
			$post_data = $this->_request->getPost();
			
			
			
			if (isset($post_data['is_hidden'])) {
				if ($post_data['is_hidden']=="1") {
					$flags->is_hidden = 1;
				} else {
					$flags->is_hidden = 0;
				}
			} else {
				$flags->is_hidden = 0;
			}
			
			$this->renderScript('exit.phtml');
			return true;
		}
	}
	
	public function showlinkAction() {
		CmsAuth::check();
		
		if (NULL === $id = $this->getRequest()->getParam('menu_id')) {
			throw new Zend_Controller_Action_Exception('Required param "Id"', 404);
		}
		
		$url_mask = MF_Main::getInstance()->_config->frontend->url_mask;
		$this->view->link_url = str_replace('\\menu_id\\',$id,$url_mask);
	}
	
}
?>